package com.huluobo.high3videoproject.setting.presenter;

import com.huluobo.base.view.BasePresenter;
import com.huluobo.high3videoproject.bean.UpdateBeanResult;
import com.huluobo.high3videoproject.setting.model.SettingModel;

import java.io.File;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class SettingPresenter extends BasePresenter<SettingContract.View> implements SettingContract.Presenter {

    private SettingModel settingModel = new SettingModel();

    @Override
    public void updateAddress(String address) {
        settingModel.updateAddress(address).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<UpdateBeanResult>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(UpdateBeanResult updateBeanResult) {
                        if (isAttach()) {
                            getView().onUpdateAddressSuccess(updateBeanResult);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (isAttach()) {
                            getView().onUpdateAddressFailed(e.getMessage());
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void updateBirth(String birth) {
        settingModel.updateBirth(birth).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<UpdateBeanResult>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(UpdateBeanResult updateBeanResult) {
                        if (isAttach()) {
                            getView().onUpdateBirthSuccess(updateBeanResult);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (isAttach()) {
                            getView().onUpdateBirthFailed(e.getMessage());
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void updateIcon(String path) {
        //1.创建文件对象
        File file = new File(path);
        //2.创建RequestBody
        RequestBody body = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        //3.封装part
        MultipartBody.Part part = MultipartBody.Part.createFormData("file", file.getName(), body);
        //4.上传
        settingModel.updateIcon(part).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<UpdateBeanResult>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(UpdateBeanResult updateBeanResult) {
                        if (isAttach()) {
                            getView().onUpdateIconSuccess(updateBeanResult);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (isAttach()) {
                            getView().onUpdateIconFailed(e.getMessage());
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void updateNickName(String nickName) {
        settingModel.updateNickName(nickName).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<UpdateBeanResult>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(UpdateBeanResult updateBeanResult) {
                        if (isAttach()) {
                            getView().onUpdateNickNameSuccess(updateBeanResult);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (isAttach()) {
                            getView().onUpdateNickNameFailed(e.getMessage());
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
